import serial

BAUDRATE = 9600

def medir() :
    port.write('M')
    print "Esperando medicao",
    medicao = port.readline()
    print medicao

def calibrar() :
    port.write('Q')
    medicao = port.readline()
    print medicao
	
if __name__ == "__main__":
    port = serial.Serial("COM3", BAUDRATE)
    print "Abriu conexao"

    # Handshake
    port.write('A')
    print "Handshake completado"
    
    while(1):
        x = raw_input()
        if (len(x) == 1 and x == 'M') :
            medir()
        elif (len(x) == 1 and x == 'Q') :
            calibrar()
        elif (len(x) == 1 and (x == 'L' or x == 'D')) :
            port.write(x);
        elif (len(x) == 1) :
            break
        elif (len(x) > 1) :
            tipo = x[0]
            cor = x[1]
            valor = x[2:]
            port.write(tipo)
            port.write(cor)
            port.write(valor)
            medir()

    port.close()
